package com.yx.cloud.map.layer;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;

import com.aaa.lib.map.MapUtils;
import com.aaa.lib.map.MapView;
import com.aaa.lib.map.area.LineArea;
import com.yx.cloud.R;
import com.yx.cloud.map.YXMapView;
import com.iot.product.sweeper.bean.AreaInfo;
import com.iot.product.sweeper.bean.MapInfo;
import com.iot.common.utils.LogUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class YXForbiddenLineLayer extends YXAreaLayer<LineArea> {
    private static final String TAG = "YXForbiddenLineLayer";
    public static boolean isForbiddenLineEditable = false;
    private Paint mPaint;       //画笔
    private Bitmap deleteIcon;  //左上角删除按钮
    private Bitmap rotateIcon;  //右上角的旋转按钮
    private float lastPosX;
    private float lastPosY;

    public YXForbiddenLineLayer(MapView mapView) {
        super(mapView);
        deleteIcon = BitmapFactory.decodeResource(mapView.getContext().getResources(), R.mipmap.restrict_delete);
        rotateIcon = BitmapFactory.decodeResource(mapView.getContext().getResources(), R.mipmap.restrict_rotate);

        area = new LineArea();

        Resources resources = mapView.getResources();
        mPaint = new Paint();
        mPaint.setColor(resources.getColor(R.color.restrict_area_stroke));
        mPaint.setStrokeWidth(5);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
    }

    @Override
    public void initAreaLayer(AreaInfo areaInfo) {
        List<Double> areaVetexs = areaInfo.getBorder();
        area.p1.x = (float) (double) areaVetexs.get(0);
        area.p1.y = (float) (double) areaVetexs.get(1);
        area.p2.x = (float) (double) areaVetexs.get(2);
        area.p2.y = (float) (double) areaVetexs.get(3);
        area.setLine(area.p1.x, area.p1.y, area.p2.x, area.p2.y);
        setAreaInfo(areaInfo);
    }

    public void initAreaLayer() {
        MapInfo map = ((YXMapView) mMapView).getMap().getMap();
        float minSize=map.getScreenSize(0.5f);
        float defaultSize= Math.max(map.width/3f,minSize);
        area.setLine(map.width / 2f - defaultSize/2, map.height / 2f, map.width / 2f + defaultSize/2, map.height / 2f);
        AreaInfo areaInfo = new AreaInfo();
        areaInfo.setType(YXAreaLayer.TYPE_FORBIDDEN_LINE);
        areaInfo.setId(((YXMapView) mMapView).getUniqueAreaId(YXAreaLayer.TYPE_FORBIDDEN_LINE));
        areaInfo.setStamp(new Date().getTime());
        areaInfo.setName("forbidline-" + areaInfo.getId());
        this.areaInfo=areaInfo;//这里不用copy ,所以直接赋值, 不用setAreaInfo
        focusId=areaInfo.getId();

        ((YXMapView) mMapView).setForbiddenAreaChange(this);
    }

    @Override
    public AreaInfo getAreaInfo() {
        ArrayList<Double> points = new ArrayList<>();
        points.add((double) area.p1.x);
        points.add((double) area.p1.y);
        points.add((double) area.p2.x);
        points.add((double) area.p2.y);
        areaInfo.setBorder(points);
        return areaInfo;
    }


    @Override
    protected boolean onTouchDown(float x, float y) {
        lastPosX = x;
        lastPosY = y;

        float scale = ((YXMapView)mMapView).getScale();
        //判断点击位置
        if (MapUtils.isPointInCircle(x, y, area.p1, deleteIcon.getWidth() / scale)) {
            operate = OP_DELETE;
            return true;
        } else if (MapUtils.isPointInCircle(x, y, area.p2, rotateIcon.getWidth() / scale)) {
            operate = OP_SCALE;
            return true;
        } else if (MapUtils.pointToLine(x, y, area.p1.x, area.p1.y, area.p2.x, area.p2.y) < 2 * deleteIcon.getWidth() / scale) {
            operate = OP_MOVE;
            return true;
        } else {
            //不在点击范围内 下一位
            operate = OP_NONE;
            return false;
        }
    }

    @Override
    protected boolean onTouchMove(float x, float y) {
        LogUtils.i("move to x: " + x + " y : " + y);
        if (operate == OP_MOVE) {
            move(x, y);
            return true;
        } else if (operate == OP_SCALE) {
            scale(x, y);
            return true;
        } else if (operate == OP_DELETE) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    protected boolean onTouchUp(float x, float y) {
        //判断点击位置
        if (operate == OP_DELETE) {
            delete();
            return true;
        } else if (operate == OP_NONE) {
            return false;
        } else {
            ((YXMapView)mMapView).handleAreaMove(this);
            return true;
        }
    }

    /**
     * 删除
     */
    private void delete() {
        mMapView.getLayerManager().removeLayer(this);
        ((YXMapView) mMapView).setForbiddenAreaChange(this);
    }

    /**
     * 缩放
     *
     * @param x 右下角拉伸到
     * @param y
     */
    private void scale(float x, float y) {

        double distance = MapUtils.distance(area.p1.x, area.p1.y, x, y);

        float scale = ((YXMapView)mMapView).getScale();
        if (distance < 2 * deleteIcon.getWidth() / scale) {
            //两条线距离太近 不操作
            return;
        }


        area.setP2(x, y);

        ((YXMapView) mMapView).setForbiddenAreaChange(this);

        mMapView.refresh();
    }


    /**
     * 平移
     *
     * @param x
     * @param y
     */
    private void move(float x, float y) {
        //计算平移距离
        float translateX = x - lastPosX;
        float translateY = y - lastPosY;

        //移动中心点

        area.setLine(area.p1.x + translateX,
                area.p1.y + translateY,
                area.p2.x + translateX,
                area.p2.y + translateY);

        //记录上次触摸点
        lastPosX = x;
        lastPosY = y;

        ((YXMapView) mMapView).setForbiddenAreaChange(this);

        mMapView.refresh();
    }

    @Override
    public void draw(Canvas canvas) {
        if (!visible) {
            return;
        }
        //        drawWithMatrix(canvas);
        drawWithMatrixValue(canvas);
    }

    private void drawWithMatrix(Canvas canvas) {
        canvas.save();

        canvas.setMatrix(mMapView.getTransform());
        drawLine(canvas);

        if (isEditable() && focusId == areaInfo.getId()) {
            drawIcon(canvas);
        }

        canvas.restore();
    }

    private void drawWithMatrixValue(Canvas canvas) {
        float scale = ((YXMapView)mMapView).getScale();

        canvas.save();
        canvas.setMatrix(mMapView.getTransform());
        drawLine1(canvas, scale);
        canvas.restore();

        if (isEditable() && focusId == areaInfo.getId()) {
            drawIcon1(canvas, scale);
        }
    }

    /**
     * 绘制矩形边框和内部的栅格线
     *
     * @param canvas
     */
    public void drawLine(Canvas canvas) {
        canvas.drawLine(area.p1.x, area.p1.y, area.p2.x, area.p2.y, mPaint);
    }

    /**
     * 绘制删除 旋转 缩放按钮
     *
     * @param canvas
     */
    private void drawIcon(Canvas canvas) {
        canvas.drawBitmap(deleteIcon, area.p1.x - deleteIcon.getWidth() / 2, area.p1.y - deleteIcon.getHeight() / 2, null);

        canvas.drawBitmap(rotateIcon, area.p2.x - rotateIcon.getWidth() / 2, area.p2.y - rotateIcon.getHeight() / 2, null);
    }

    private void drawIcon1(Canvas canvas, float scale) {
        getTransformedPoint(mMapView.getTransform(), area.p1.x - deleteIcon.getWidth() / 2f / scale, area.p1.y - deleteIcon.getHeight() / 2f / scale);
        canvas.drawBitmap(deleteIcon, tmpPoint[0], tmpPoint[1], null);
        getTransformedPoint(mMapView.getTransform(), area.p2.x - rotateIcon.getWidth() / 2f / scale, area.p2.y - rotateIcon.getHeight() / 2f / scale);
        canvas.drawBitmap(rotateIcon, tmpPoint[0], tmpPoint[1], null);
    }

    public void drawLine1(Canvas canvas, float scale) {
        mPaint.setPathEffect(new DashPathEffect(new float[]{15 / scale, 8 / scale}, 0));
        mPaint.setStrokeWidth(5 / scale);
        canvas.drawLine(area.p1.x, area.p1.y, area.p2.x, area.p2.y, mPaint);
    }

    @Override
    public boolean isEditable() {
        return isForbiddenLineEditable;
    }

}
